package dbmanager;


import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
//import java.util.ArrayList;


import domain.*;


/**
 * Trieda pre pracu s sql dotazmi.
 * @author PeterCagan
 * 
 */
public class SQLManager {
	

	private Connection con;
	
	
	/**
	 * Konstruktor pre vytvorenie instancie triedy SQLManager. 
	 * @throws  
	 */
	public SQLManager(Connection c){
		this.con = c;
	}
	
	/**
	 * Metoda na vratenie pripojenia 
	 */
	public Connection getConnection(){
		return this.con;
	}
	
	/**
	 * Funkcia predstavujuca INSERT do tabulky. 
	 * @param schema - nazov schemy v ktorej sa tabulka nachadza
	 * @param tabval - nazov tabulky, atributy a ich hodnoty. Format: ".TABLE(atribut,atribut,...) VALUE(hodnota,hodnota,...)"
	 */
	public void insert(String schema, String tabval){
				String ins= "INSERT INTO " + schema + tabval;
		try {
			Statement st = this.con.createStatement();
			st.execute(ins);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * Funkcia predstavujuca INSERT do tabulky KLIENT pre uzly. 
	 * @param klient - objekt typu KLIENT.
	 * @param schema - nazov schemy v ktorej sa tabulka nachadza
	 */
	public void insertN(KLIENT klient, String schema){
		String ins = null;
		if(klient.getDatZac().getA() != null){
			ins = "INSERT INTO " + schema + klient.getInsTabValN();
		}else {
			ins = "INSERT INTO " + schema + klient.getInsTabVal();
		}
		//Integer idk = 0;
		try {
			Statement st = this.con.createStatement();
			st.execute(ins);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * Funkcia predstavujuca INSERT do tabulky UCET pre uzly. 
	 * @param klient - objekt typu UCET.
	 * @param schema - nazov schemy v ktorej sa tabulka nachadza
	 */
	public void insertN(UCET ucet, String schema){
		String ins= "INSERT INTO " + schema + ucet.getInsTabValN();
		Integer idu = 0;
		try {
			Statement st = this.con.createStatement();
			ResultSet r = null;
			st.execute(ins, new String[] {"ID_UCTU"});
			r = st.getGeneratedKeys();
			if(r.next()){
				idu = r.getInt(1);
				ucet.getIDuctu().setIA(idu);}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * Predstavuje SQL prikaz SELECT. Ako parametre prebera retazce predstavujuce nazvy stlpcov, ktore sa maju vybrat,
	 * nazvy tabuliek a podmienky
	 * @return Hodnota typu ResultSet
	 */
	public ResultSet SELECT(String a, String t, String p){
		ResultSet r = null;
		try {
			Statement st = this.con.createStatement();
			r = st.executeQuery("SELECT " + a 
							   + " FROM	" + t
							   + " WHERE " + p);
			/**/System.out.println("Zaznam bol vybraty:");
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return r;
	}

	public KLIENT HladajKlienta(KLIENT k) {
		//ResultSet r = this.SELECT("*", "BA.Klient k", "k.meno = '" + k.getMeno().getA() + "' and k.priezvisko = '" + k.getPriezv().getA() + "' and k.rodne_cislo = '" + k.getRodC().getA() + "'");
		//String atrib = "*";
		//String tab = "BA.KLIENT ";
		//String podm = "k.meno = '" + k.getMeno().getA(); //+ "' and k.priezvisko = '" + k.getPriezv().getA() + "' and k.rodne_cislo = '" + k.getRodC().getA() + "'";
		
		ResultSet r = null;
		try {
			Statement s= this.con.createStatement();
			r = s.executeQuery("select * from BA.Klient k where k.meno = '" + k.getMeno().getA() + "' and k.priezvisko = '" + k.getPriezv().getA() + "' and k.rodne_cislo = '" + k.getRodC().getA() + "'");
			while (r.next()){
				k.getIDklienta().setIA(r.getInt(1));
				k.getIDpob().setIA(r.getInt("Id_pobocka"));
				k.getAdresa().setA(r.getString("Adresa"));
				k.getEmail().setA(r.getString("Email"));
				k.getTelC().setA(r.getString("Tel_cislo"));
				k.getDatZac().setA(r.getString("Dat_zac"));
				k.getDatKon().setA(r.getString("Dat_kon"));
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return k;
		}
		return k;
	}
		// TODO Auto-generated method stub

		public KLIENT vybKLIENTupd(KLIENT k){
			String atrib = "*";
			String tab = "BA.KLIENT k";
			String podm = "k.meno = '" + k.getMeno().getA() + "' and k.priezvisko = '" + k.getPriezv().getA() + "' and k.rodne_cislo = '" + k.getRodC().getA() + "'";
			ResultSet r = SELECT(atrib,tab,podm);
			KLIENT kl = null;
			try {
			while(r.next()){
			kl = new KLIENT(r.getInt(1),r.getInt(2),r.getString(3),r.getString(4),r.getString(5),r.getString(6),r.getString(7),r.getString(8),r.getString(9),"");
			
			}
			} catch (SQLException e) {
			e.printStackTrace();
			}
			return kl;
		}
		
		public Integer vratMaxID(String schema, String tab){
			Integer s = null;
			ResultSet r = null;
			try {
			Statement st = this.con.createStatement();
			r = st.executeQuery("SELECT MAX(ID_Klient) " + " FROM " + schema +"." + tab + " WHERE ID_Pobocka = 1");
			if(r.next()){
			s=r.getInt(1);
			}
			//System.out.println("Zaznam bol vybraty:" + s);
			} catch (SQLException e) {
			e.printStackTrace();
			}
			return s;
		}
		
	}
	
	/**
	 * Vyberie udaje o pobocke podla atributu ID_POBOCKY.
	 * @param idp - Hodnota atributu  ID_POBOCKY.
	 * @return Hodnota typu POBOCKA.
	 */


	

	

